package com.imooc.demo.service.impl;

import com.imooc.demo.dao.Category2Dao;
import com.imooc.demo.entity.Category2;
import com.imooc.demo.service.Category2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class Category2ServiceImpl implements Category2Service {
    @Autowired
    private Category2Dao category2Dao;

    @Override
    public List<Category2> getCategory2List() {
        return category2Dao.queryCategory2();
    }

    @Override
    public Category2 getCategory2ById(int category2Id) {
        //测试用 （1不能除以0）：int a = 1/0;在浏览器输入（直接调用方法getCategory2ById，设置参数为1）：http://localhost:8080/demo/superadmin2/getcategory1byid?category1Id=1
        return category2Dao.getQueryCategory2ById(category2Id);
    }

    //获取RuntimeException并回滚
    @Transactional
    @Override
    public boolean addCategory2(Category2 category2) {
        if(category2.getCategory2Name()!=null && !"".equals(category2.getCategory2Name())){
            try{
                int effectedNum = category2Dao.insertCategory2(category2);
                if(effectedNum>0){
                    return  true;
                }else{
                    throw  new RuntimeException("插入种类信息失败2！");
                }
            }catch (Exception e){
                throw new RuntimeException("插入种类信息失败2！");
            }
        }else{
            throw new RuntimeException("种类信息不能为空2！");
        }
    }

    @Override
    public boolean modifyCategory2(Category2 category2) {
        //空值判断，主要是category1Id不能为空
        if(category2.getCategory2Id()!=null &&category2.getCategory2Id()>0) {
            try {
                //更新种类信息
                int effectedNum = category2Dao.updateCategory2(category2);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("更新种类信息失败！2");
                }
            } catch (Exception e) {
                throw new RuntimeException("更新种类信息失败2：" + e.toString());
            }
        }else{
            throw new RuntimeException("种类信息不能为空！2");
        }
    }

    @Override
    public boolean deleteCategory2(int category2Id) {
        if (category2Id > 0) {
            try {
                //删除种类信息
                int effectedNum = category2Dao.deleteCategory2(category2Id);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("删除种类信息失败！2");
                }
            } catch (Exception e) {
                throw new RuntimeException("删除种类信息失败2：" + e.toString());
            }
        } else {
            throw new RuntimeException("种类ID不能为空！2");
        }
    }

    @Override
    public List<Category2> getCategory2ByCategory1Id(Integer category10Id) {
        //测试用 （1不能除以0）：int a = 1/0;在浏览器输入（直接调用方法getCategory2ById，设置参数为1）：http://localhost:8080/demo/superadmin2/getcategory1byid?category1Id=1
        return category2Dao.getCategory2ByCategory1Id(category10Id);
    }

    @Override///模糊搜索
    public List<Category2> searchCategory2(String category2Name) {
        return category2Dao.searchCategory2(category2Name);
    }


}
